/*
 *      dxtabitem.cpp
 *
 *      Copyright 2010 David Vachulka <david@konstrukce-cad.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

#ifdef WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shellapi.h>
#endif
#include "dxtabitem.h"
#include "tetristabitem.h"
#include "icons.h"
#include "config.h"
#include "i18n.h"
#include "dxutils.h"
#include <FX88591Codec.h>
#include <FXUTF16Codec.h>

FXDEFMAP(dxTabItem) dxTabItemMap[] = {
    FXMAPFUNC(SEL_TEXTLINK,             dxTabItem_TEXT,     dxTabItem::onTextLink),
    FXMAPFUNC(SEL_RIGHTBUTTONRELEASE,   dxTabItem_TEXT,     dxTabItem::onTextRightMouse),
    FXMAPFUNC(SEL_MIDDLEBUTTONRELEASE,  dxTabItem_TEXT,     dxTabItem::onTextMiddleMouse),
    FXMAPFUNC(SEL_COMMAND,              dxTabItem_OPENLINK, dxTabItem::onOpenLink),
    FXMAPFUNC(SEL_COMMAND,              dxTabItem_COPYLINK, dxTabItem::onCopyLink),
    FXMAPFUNC(SEL_CLIPBOARD_REQUEST,    0,                  dxTabItem::onClipboardRequest)
};

FXIMPLEMENT(dxTabItem, dxEXTabItem, dxTabItemMap, ARRAYNUMBER(dxTabItemMap))

dxTabItem::dxTabItem(dxTabBook *tab, const FXString &tabtext, FXIcon *icon, FXuint opts,
        FXint id)
    : dxEXTabItem(tab, tabtext, icon, opts), m_id(id)
{
    m_link = "";
    this->setIconPosition(ICON_BEFORE_TEXT);

    m_text = new dxText(tab, this, dxTabItem_TEXT, FRAME_SUNKEN|LAYOUT_FILL_X|LAYOUT_FILL_Y);
}

dxTabItem::~dxTabItem()
{
}

long dxTabItem::onTextLink(FXObject *, FXSelector, void *data)
{
    dxutils::launchLink(static_cast<FXchar*>(data));
    return 1;
}

long dxTabItem::onTextRightMouse(FXObject *, FXSelector, void *ptr)
{
    FXEvent* event=(FXEvent*)ptr;
    if(event->moved) return 1;
    FXbool link = m_text->isXYLink(event->win_x, event->win_y);
    FXMenuPane popupmenu(this);
    if(link)
    {
        m_link = m_text->getLinkTextXY(event->win_x, event->win_y);
        new FXMenuCommand(&popupmenu, _("Open in a web browser"), NULL, this, dxTabItem_OPENLINK);
        new FXMenuCommand(&popupmenu, _("Copy the link"), NULL, this, dxTabItem_COPYLINK);
        new FXMenuSeparator(&popupmenu);
    }
    FXMenuCommand *clear = new FXMenuCommand(&popupmenu, _("Clear window"), ICO_CLEAR, m_text, dxText::ID_CLEAR);
    new FXMenuSeparator(&popupmenu);
    FXMenuCommand *selectall = new FXMenuCommand(&popupmenu, _("Select All"), ICO_SELECTALL, m_text, dxText::ID_SELECT_ALL);
    FXMenuCommand *copy = new FXMenuCommand(&popupmenu, _("Copy"), ICO_COPY, m_text, dxText::ID_COPY_SEL);
    new FXMenuSeparator(&popupmenu);
    FXMenuCommand *find = new FXMenuCommand(&popupmenu, _("Find"), ICO_FIND, m_text, dxText::ID_SEARCH);
    if(!m_text->hasSomeText())
    {
        clear->disable();
        selectall->disable();
        copy->disable();
        find->disable();
    }
    if(!m_text->hasSelectedText())
    {
        copy->disable();
    }
    popupmenu.create();
    popupmenu.popup(NULL,event->root_x,event->root_y);
    getApp()->runModalWhileShown(&popupmenu);
    return 1;
}

long dxTabItem::onTextMiddleMouse(FXObject*, FXSelector, void *ptr)
{
    FXEvent* event=(FXEvent*)ptr;
    if(event->moved) return 1;
#ifndef WIN32 //middleclick copy
    m_text->copySelectedText();
#endif
    return 1;
}

long dxTabItem::onOpenLink(FXObject*, FXSelector, void*)
{
    dxutils::launchLink(m_link);
    return 1;
}

long dxTabItem::onCopyLink(FXObject*, FXSelector, void*)
{
    FXDragType types[4];
    types[0]=stringType;
    types[1]=textType;
    types[2]=utf8Type;
    types[3]=utf16Type;
    acquireClipboard(types,4);
    return 1;
}

// Somebody wants our selection
long dxTabItem::onClipboardRequest(FXObject *sender, FXSelector sel, void *ptr)
{
    FXEvent *event = (FXEvent*)ptr;
    // Try handling it in base class first
    if(FXWindow::onClipboardRequest(sender, sel, ptr)) return 1;
    // Requested data from clipboard
    if(event->target==stringType || event->target==textType || event->target==utf8Type || event->target==utf16Type)
    {
        // Expand newlines to CRLF on Windows
#ifdef WIN32
        unixToDos(m_link);
#endif
        // Return clipped text as as UTF-8
        if(event->target==utf8Type)
        {
            FXTRACE((100, "Request UTF8\n"));
            setDNDData(FROM_CLIPBOARD, event->target, m_link);
            return 1;
        }
        // Return clipped text translated to 8859-1
        if(event->target==stringType || event->target==textType)
        {
            FX88591Codec ascii;
            FXTRACE((100, "Request ASCII\n"));
            setDNDData(FROM_CLIPBOARD, event->target, ascii.utf2mb(m_link));
            return 1;
        }
        // Return text of the selection translated to UTF-16
        if(event->target==utf16Type)
        {
            FXUTF16LECodec unicode;             // FIXME maybe other endianness for unix
            FXTRACE((100, "Request UTF16\n"));
            setDNDData(FROM_CLIPBOARD, event->target, unicode.utf2mb(m_link));
            return 1;
        }
    }
    return 0;
}
